---
title: "CESS Fear Videos Pilot"
author: "Deshawn Sambrano"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output: 
  html_document:
    toc: yes
    self_contained: yes
---

<style type="text/css">
.table {

    width: 70%;
    margin: auto;

}
</style>

```{r setup, include=FALSE}
require(reshape2)
require(Hmisc)
require(psych)
require(ggplot2)
require(ggpubr)
require(cowplot)
require(gridExtra)
require(grid)
require(ggExtra)
require(ggsci)
require(latex2exp)
require(RColorBrewer)
require(knitr)
knitr::opts_chunk$set(echo = FALSE, fig.align="center")
knitr::opts_chunk$set(cache=FALSE)
# knitr::opts_chunk$set(out.width = 1)
options(width = 110)

# Creating theme for APA in ggplot
apatheme <- theme(panel.grid.major=element_blank(),
                  panel.grid.minor=element_blank(),
                  panel.border=element_blank(),
                  panel.background = element_rect(fill = "transparent"),#
                  axis.line=element_line(),
                  text=element_text(family='sans'),#
                  plot.title = element_text(hjust = 0.5),
                  plot.subtitle = element_text(hjust = 0.5),
                  legend.key = element_rect(fill = "transparent", colour = "transparent", size = 0.25),
                  legend.box.background = element_rect(fill = "transparent", linetype="solid"),#
                  plot.background = element_rect(fill = "transparent",colour = NA))#

```

## Reading in Qualtics Data

```{r qualtrics_read}
raw_qual_dir <- "../01_Data/cess_pilot_qualtrics/raw"
clean_qual_dir <- "../01_Data/cess_pilot_qualtrics/clean"

f_name <- list.files(path = raw_qual_dir, pattern = "*csv")
x <- read.csv(list.files(path = raw_qual_dir, pattern = "*csv", full.names = TRUE))

# Remove info from Qualtrics
x <- x[-c(1,2),]
# x <- x[order(x$ID),]

# Save file
new_fname <- paste0(substring(f_name,1,nchar(f_name)-4),'_CLEAN',
                    substring(f_name,nchar(f_name)-3,nchar(f_name)))
write.csv(x, paste0(clean_qual_dir, new_fname), row.names = FALSE)

# Read it in again so the numerics are read in now unlike previous time
qual_data <- read.csv(paste0(clean_qual_dir, new_fname))

# Remove those that didn't finish and save again
qual_data <- qual_data[qual_data$Progress == 100,] 
qual_data <- qual_data[qual_data$problems != 'yes',] 
write.csv(qual_data, paste0(clean_qual_dir, new_fname), row.names = FALSE)
```

## Stats and Figure 17

```{r Emotions, warning=FALSE}
fear <- qual_data[,c('Pre.Emotions_3', 'V.Emotions_3', 'A.Emotions_3')]
fear_stats <- describe(fear)
varNames <- c('Pre','Video','Audio')


fear_long_data <- data.frame(rating=c(as.matrix(fear)),
                             time=rep(varNames,each=nrow(fear)))

anova(aov(fear_long_data$rating~fear_long_data$time))
t.test(rating ~ time, data = subset(fear_long_data,fear_long_data$time %in% c('Pre','Video')))
t.test(rating ~ time, data = subset(fear_long_data,fear_long_data$time %in% c('Pre','Audio')))

limits <- aes(ymax = mean + (1.645*se), ymin=mean - (1.645*se)) # 90% Confidence intervals

# limits <- aes(ymax = mean + (1.96*se), ymin=mean - (1.96*se)) # 95% Confidence intervals

dodge <- position_dodge(width=0.9)
j_dodge <- position_jitterdodge(dodge.width=.9, jitter.width = 1.5)


# Bar Graph
# Help from: http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/
# p <- ggbarplot(fear_long_data, x='time', y='rating', fill='time', palette='jco',
#                add = c('mean_se'), alpha=.6) +
#         geom_jitter(data = fear_long_data, position=j_dodge,show.legend=FALSE,
#                aes(x = as.numeric(time), y = rating, fill=time, color=time)) +
#         stat_compare_means(method = 'anova', label.y=105) +
#         ylab('Emotion Rating') +
#         xlab('Music') +
#         labs(fill = "Music") +
#         theme(legend.key = element_rect(colour = NA))
# p

p=ggplot(fear_stats,aes(x=factor(varNames, levels=varNames),y=mean,fill=factor(varNames, levels=varNames)))+
  geom_bar(stat="identity", alpha=.6)+
  scale_fill_jco()+
    geom_jitter(data = fear_long_data, position=j_dodge,show.legend=FALSE,
               aes(x = factor(time, levels=varNames), y = rating, fill=factor(time, levels=varNames), color=factor(time, levels=varNames))) +
  scale_color_jco()+
  geom_errorbar(limits, width=0.25)+
  ylab('Emotion Rating')+
  xlab('Time')+labs(fill="Time")+theme(legend.key = element_rect(colour =NA))+apatheme
p
ggsave(p, filename = paste0("plots/FearOverTime.png"),  bg = "transparent")


```

## Stats on Fear Ratings for Audio and Video Stimuli

Both Visually and Statistically we see that the video made a large effect consistent with the previous pilot. The audio clip did not have as strong of an effect but was still significant. Interestingly though, their were many people who were not affected by the audio stimili. 

```{r Audio, warning=FALSE}
resp_levels <- c("Not at all", "Somewhat", "Very much so", "Absolutely")

p <-ggplot(data=subset(qual_data,qual_data$A_frighten != ""),aes(factor(A_frighten,levels=resp_levels))) + 
        geom_bar() + scale_fill_jco() +
        xlab('Response Options') +
        ylab('Frequency') + ggtitle('Frightful')
p
ggsave(p, filename = paste0("plots/AudioFear.png"),  bg = "transparent")

p <-ggplot(data=subset(qual_data,qual_data$A.unpleasant != ""),aes(factor(A.unpleasant,levels=resp_levels))) +
        geom_bar() + scale_fill_jco() +
        xlab('Response Options') +
        ylab('Frequency') + ggtitle('Unpleasant')
p
ggsave(p, filename = paste0("plots/AudioUnpleasant.png"),  bg = "transparent")
```

## Further Analysis of Audio
Because there were many people that were not affected (as far as their fear levels), I further investigated what they felt about the stimuli. Though not all indicated that it was frightening, the majority of people indicated that if they heard it multiple times at a loud volume they would find it unpleasant. This taken with the fact that the video is the primary emotion induction methods and the audio is only meant to maintain the emotions over the course of the experiment. These data make me confident in the selected method. 


## Conclusion
We are good to start with a walk through to prep everything. And the start running subjects.  



## Appendix

As a reminder, here is the audio and video clip. 

## Fear Induction

### Stimili
<!-- [![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/YOUTUBE_VIDEO_ID_HERE/0.jpg)](https://www.youtube.com/watch?v=YOUTUBE_VIDEO_ID_HERE) -->


**Fear Video**

<video width="560" height="315" style="margin: 0 auto; display: block;" controls>
      <source src="../../SupportDocs/Videos/fear.mov" type="video/mp4">
</video>


**Fear Audio Clip** 

<audio controls>
  <source src="../../SupportDocs/Videos/fear_audio.wav" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

